package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.PigWeightDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.PigWeight;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for PigWeight entities
 * 
 */

@Service("PigWeightService")

@Transactional
public class PigWeightServiceImpl implements PigWeightService {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages PigWeight entities
	 * 
	 */
	@Autowired
	private PigWeightDAO pigWeightDAO;

	/**
	 * Instantiates a new PigWeightServiceImpl.
	 *
	 */
	public PigWeightServiceImpl() {
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public PigWeight deletePigWeightPig(Integer pigweight_id, Integer related_pig_id) {
		PigWeight pigweight = pigWeightDAO.findPigWeightByPrimaryKey(pigweight_id, -1, -1);
		Pig related_pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		pigweight.setPig(null);
		related_pig.getPigWeights().remove(pigweight);
		pigweight = pigWeightDAO.store(pigweight);
		pigWeightDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		pigDAO.remove(related_pig);
		pigDAO.flush();

		return pigweight;
	}

	/**
	 */
	@Transactional
	public PigWeight findPigWeightByPrimaryKey(Integer id) {
		return pigWeightDAO.findPigWeightByPrimaryKey(id);
	}

	/**
	 * Save an existing PigWeight entity
	 * 
	 */
	@Transactional
	public void savePigWeight(PigWeight pigweight) {
		PigWeight existingPigWeight = pigWeightDAO.findPigWeightByPrimaryKey(pigweight.getId());

		if (existingPigWeight != null) {
			if (existingPigWeight != pigweight) {
				existingPigWeight.setId(pigweight.getId());
				existingPigWeight.setWeight(pigweight.getWeight());
				existingPigWeight.setTime(pigweight.getTime());
				existingPigWeight.setMemo(pigweight.getMemo());
			}
			pigweight = pigWeightDAO.store(existingPigWeight);
		} else {
			pigweight = pigWeightDAO.store(pigweight);
		}
		pigWeightDAO.flush();
	}

	/**
	 * Return all PigWeight entity
	 * 
	 */
	@Transactional
	public List<PigWeight> findAllPigWeights(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<PigWeight>(pigWeightDAO.findAllPigWeights(startResult, maxRows));
	}

	/**
	 * Return a count of all PigWeight entity
	 * 
	 */
	@Transactional
	public Integer countPigWeights() {
		return ((Long) pigWeightDAO.createQuerySingleResult("select count(o) from PigWeight o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public PigWeight savePigWeightPig(Integer id, Pig related_pig) {
		PigWeight pigweight = pigWeightDAO.findPigWeightByPrimaryKey(id, -1, -1);
		Pig existingpig = pigDAO.findPigByPrimaryKey(related_pig.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpig != null) {
			existingpig.setId(related_pig.getId());
			existingpig.setNumber(related_pig.getNumber());
			existingpig.setEarNumber(related_pig.getEarNumber());
			existingpig.setRfid(related_pig.getRfid());
			existingpig.setBirthday(related_pig.getBirthday());
			existingpig.setState(related_pig.getState());
			existingpig.setMemo(related_pig.getMemo());
			related_pig = existingpig;
		}

		pigweight.setPig(related_pig);
		related_pig.getPigWeights().add(pigweight);
		pigweight = pigWeightDAO.store(pigweight);
		pigWeightDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		return pigweight;
	}

	/**
	 * Delete an existing PigWeight entity
	 * 
	 */
	@Transactional
	public void deletePigWeight(PigWeight pigweight) {
		pigWeightDAO.remove(pigweight);
		pigWeightDAO.flush();
	}

	/**
	 * Load an existing PigWeight entity
	 * 
	 */
	@Transactional
	public Set<PigWeight> loadPigWeights() {
		return pigWeightDAO.findAllPigWeights();
	}
}
